/*
 * my_main.c
 *
 *  Created on: 2021年9月17日
 *      Author: wuwenfeng
 */
#include "my_main.h"
#include "button.h"

#define HC595_DCK(x) HAL_GPIO_WritePin(HC595_DCK_GPIO_Port, HC595_DCK_Pin, x);
#define HC595_RCK(x) HAL_GPIO_WritePin(HC595_RCK_GPIO_Port, HC595_RCK_Pin, x);
#define HC595_SCK(x) HAL_GPIO_WritePin(HC595_SCK_GPIO_Port, HC595_SCK_Pin, x);

#define MOTA(x) HAL_GPIO_WritePin(MOTA_GPIO_Port, MOTA_Pin, x);
#define MOTB(x) HAL_GPIO_WritePin(MOTB_GPIO_Port, MOTB_Pin, x);

#define KEY4 HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin)
#define KEY1 HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin)
#define KEY2 HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin)
#define KEY3 HAL_GPIO_ReadPin(KEY4_GPIO_Port, KEY4_Pin)

struct button key1,key2,key3,key4,overload;

extern ADC_HandleTypeDef hadc;
const char d_num_data[2][10]=
{
	{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f},
	{0xed,0x48,0xe6,0x6e,0x4b,0x2f,0xaf,0x68,0xef,0x6f}
};

struct
{
	char d_num[4];
	char dot1:1;
	char dot2:1;
	char dot3:1;
	char dot4:1;
	char led_run:1;
	char led_p:1;
	char led_n:1;
	char led_err:1;
}dis_buff;

void Send_to_595(char h,char l)
{

	for(int a=0;a<8;a++)
	{
		if((h<<a)&0x80)
		{
			HC595_DCK(1);
		}else
		{
			HC595_DCK(0);
		}
		HC595_SCK(1);
		HC595_SCK(0);
	}

	for(int a=0;a<8;a++)
	{
		if((l<<a)&0x80)
		{
			HC595_DCK(1);
		}else
		{
			HC595_DCK(0);
		}
		HC595_SCK(1);
		HC595_SCK(0);

	}
	HC595_RCK(1);
	HC595_RCK(0);
}

void display()
{
	char h_buff=0,l_buff=0;


	h_buff=0,l_buff=0;
	Send_to_595(h_buff,l_buff);
	h_buff=~0x01;
	l_buff=dis_buff.d_num[0]>=10?0:d_num_data[0][dis_buff.d_num[0]];
	if(dis_buff.dot1==1)
	{
		l_buff|=0x80;
	}
	Send_to_595(h_buff,l_buff);

	h_buff=0,l_buff=0;
	Send_to_595(h_buff,l_buff);
	h_buff=~0x80;
	l_buff=dis_buff.d_num[1]>=10?0:d_num_data[1][dis_buff.d_num[1]];
	if(dis_buff.dot2==1)
	{
		l_buff|=0x10;
	}
	Send_to_595(h_buff,l_buff);

	h_buff=0,l_buff=0;
	Send_to_595(h_buff,l_buff);
	h_buff=~0x40;
	l_buff=dis_buff.d_num[2]>=10?0:d_num_data[0][dis_buff.d_num[2]];
	if(dis_buff.dot3==1)
	{
		l_buff|=0x80;
	}
	Send_to_595(h_buff,l_buff);

	h_buff=0,l_buff=0;
	Send_to_595(h_buff,l_buff);
	h_buff=0xC1;
	l_buff=dis_buff.d_num[3]>=10?0:d_num_data[1][dis_buff.d_num[3]];
	if(dis_buff.dot4==1)
	{
		l_buff|=0x10;
	}
	if(dis_buff.led_run==1)
	{
		h_buff|=0x10;
	}
	if(dis_buff.led_err==1)
	{
		h_buff|=0x08;
	}
	if(dis_buff.led_n==1)
	{
		h_buff|=0x04;
	}
	if(dis_buff.led_p==1)
	{
		h_buff|=0x02;
	}
	Send_to_595(h_buff,l_buff);
}

long countdown=0;
long countdown_set=15000;

void mymain()
{
	uint32_t runtime=0,move=0;
	uint8_t mode=0,overload_mode=0;
	uint16_t adc,adc_times=0;
	uint32_t adc_l;
	uint16_t overload_times=0;
	MOTA(0);
	MOTB(0);
	HC595_DCK(0);
	HC595_RCK(0);
	HC595_SCK(0);

	dis_buff.d_num[0]=0xff;
	dis_buff.d_num[1]=0xff;
	dis_buff.d_num[2]=0xff;
	dis_buff.d_num[3]=0xff;

	countdown=1000;

	while(1)
	{

		switch(mode)
		{
		case 0:
			//Startup
			if(HAL_GetTick()>move)
			{
				move=HAL_GetTick()+100;
				countdown-=100;
				if(countdown<0)
				{
					mode=1;
				}

				HAL_ADC_Start(&hadc);
				HAL_ADC_GetValue(&hadc);
				HAL_ADC_Stop(&hadc);

				dis_buff.d_num[0]=((countdown/100)%10);
				dis_buff.d_num[1]=((countdown/100)%10);
				dis_buff.d_num[2]=((countdown/100)%10);
				dis_buff.d_num[3]=((countdown/100)%10);

				dis_buff.led_err=countdown>>0;
				dis_buff.led_p=countdown>>1;
				dis_buff.led_n=countdown>>2;
				dis_buff.dot1=countdown>>3;
				dis_buff.dot2=countdown>>4;
				dis_buff.dot3=countdown>>5;
				dis_buff.dot4=countdown>>6;

			}
			break;
		case 1:
			//standby
			MOTA(0);
			MOTB(0);
			dis_buff.d_num[0]=0xff;
			dis_buff.d_num[1]=0xff;
			dis_buff.d_num[2]=0xff;
			dis_buff.d_num[3]=0xff;
			dis_buff.led_err=0;
			dis_buff.led_p=0;
			dis_buff.led_n=0;
			dis_buff.dot1=0;
			dis_buff.dot2=0;
			dis_buff.dot3=0;
			dis_buff.dot4=0;
			overload_times=0;
			if(key2.code!=0)
			{
				mode=2;
				countdown=countdown_set;
			}
			if(key3.code!=0)
			{
				mode=3;
				countdown=countdown_set;
			}
			if(key1.code!=0)
			{
				mode=4;
				countdown=10000;
			}
			break;
		case 2:
			MOTA(0);
			MOTB(1);
			if(HAL_GetTick()>move)
			{
				move=HAL_GetTick()+100;
				if(dis_buff.led_p==1)
				{
					dis_buff.led_p=0;
				}else
				{
					dis_buff.led_p=1;
				}
				countdown-=100;
				if(countdown<0)
				{
					mode=1;
				}
			}
			dis_buff.d_num[3]=(countdown/100)%10;
			dis_buff.d_num[2]=(countdown/1000)%10;
			dis_buff.d_num[1]=((countdown/10000)%10);
			dis_buff.d_num[1]=dis_buff.d_num[1]==0?255:dis_buff.d_num[1];
			dis_buff.dot3=1;
			dis_buff.led_n=0;
			if(key3.code!=0)
			{
				mode=3;
				countdown=countdown_set-countdown;
			}
			if(key4.code<0)
			{
				mode=1;
			}
			if(overload.code!=0)
			{
				overload_times+=1;
			}
			if(overload_times>2)
			{
				overload_mode=2;
				mode=5;
			}
			break;
		case 3:
			MOTB(0);
			MOTA(1);
			if(HAL_GetTick()>move)
			{
				move=HAL_GetTick()+100;
				if(dis_buff.led_n==1)
				{
					dis_buff.led_n=0;
				}else
				{
					dis_buff.led_n=1;
				}
				countdown-=100;
				if(countdown<0)
				{
					mode=1;
				}
			}
			dis_buff.d_num[3]=(countdown/100)%10;
			dis_buff.d_num[2]=(countdown/1000)%10;
			dis_buff.d_num[1]=((countdown/10000)%10);
			dis_buff.d_num[1]=dis_buff.d_num[1]==0?255:dis_buff.d_num[1];
			dis_buff.dot3=1;
			dis_buff.led_p=0;
			if(key2.code!=0)
			{
				mode=2;
				countdown=countdown_set-countdown;
			}
			if(key4.code<0)
			{
				mode=1;
			}
			if(overload.code!=0)
			{
				overload_times+=1;
			}
			if(overload_times>2)
			{
				overload_mode=3;
				mode=5;
			}
			break;
		case 4:
			//setting mode
			if(HAL_GetTick()>move)
			{
				move=HAL_GetTick()+100;
				if(dis_buff.dot1==1)
				{
					dis_buff.dot1=0;
				}else
				{
					dis_buff.dot1=1;
				}
				countdown-=100;
				if(countdown<0)
				{
					mode=1;
				}

				if(key2.code<0){countdown_set+=1000;countdown=10000;}
				if(key3.code<0){countdown_set-=1000;countdown=10000;}

			}

			if(key2.code>0){countdown_set+=100;countdown=10000;}
			if(key3.code>0){countdown_set-=100;countdown=10000;}
			if(countdown_set<100){countdown_set=100;}
			if(countdown_set>60000){countdown_set=60000;}

			if(key1.code!=0){mode=1;}
			dis_buff.d_num[3]=(countdown_set/100)%10;
			dis_buff.d_num[2]=(countdown_set/1000)%10;
			dis_buff.d_num[1]=((countdown_set/10000)%10);
			dis_buff.d_num[1]=dis_buff.d_num[1]==0?255:dis_buff.d_num[1];
			dis_buff.dot3=1;

			break;
		case 5:
			//overload
			MOTB(0);
			MOTA(0);
			overload_times=0;
			dis_buff.d_num[3]=(countdown/100)%10;
			dis_buff.d_num[2]=(countdown/1000)%10;
			dis_buff.d_num[1]=((countdown/10000)%10);
			dis_buff.d_num[1]=dis_buff.d_num[1]==0?255:dis_buff.d_num[1];
			dis_buff.dot3=1;
			dis_buff.led_err=1;
			if(key4.code!=0){mode=1;}

			if(key2.code!=0)
			{
				mode=2;
				if(overload_mode==2)
				{

				}else
				{
					countdown=countdown_set-countdown;
				}

			}
			if(key3.code!=0)
			{
				mode=3;
				if(overload_mode==3)
				{

				}else
				{
					countdown=countdown_set-countdown;
				}

			}

			break;
		}

		if(HAL_GetTick()>runtime)
		{
			runtime+=1000;
/*
			dis_buff.d_num[0]=rand()%10;
			dis_buff.d_num[1]=rand()%10;
			dis_buff.d_num[2]=rand()%10;
			dis_buff.d_num[3]=rand()%10;
			dis_buff.dot1=rand()%2;
			dis_buff.dot2=rand()%2;
			dis_buff.dot3=rand()%2;
			dis_buff.dot4=rand()%2;
			dis_buff.led_err=rand()%2;
			dis_buff.led_n=rand()%2;
			dis_buff.led_p=rand()%2;
			dis_buff.led_run=rand()%2;
*/
			if(dis_buff.led_run==1)
			{
				dis_buff.led_run=0;
			}else
			{
				dis_buff.led_run=1;
			}
		}
		HAL_ADC_Start(&hadc);
		adc_l+=HAL_ADC_GetValue(&hadc);
		adc_times+=1;
		HAL_ADC_Stop(&hadc);
		if(adc_times==32)
		{
			adc=adc_l>>5;
			adc_times=0;
			adc_l=0;
		}
		if(adc>600)
		{
			GEI_BUTTON_CODE(&overload,1);
		}else
		{
			GEI_BUTTON_CODE(&overload,0);
		}
		GEI_BUTTON_CODE(&key1,KEY1);
		GEI_BUTTON_CODE(&key2,KEY2);
		GEI_BUTTON_CODE(&key3,KEY3);
		GEI_BUTTON_CODE(&key4,KEY4);

		display();
	}

}
